package com.simple.study.base.controller;

import com.github.pagehelper.PageInfo;
import com.simple.study.entity.PageResult;
import com.simple.study.entity.Result;
import com.simple.study.entity.ShopResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * <br/>
 * 基础控制层接口
 * </p>
 */
public class BaseController {
	public BaseController() {
	}

	public Result result(Object data) {
		return new Result(200, "操作成功", data);
	}

	public Result result(Integer code, String message, Object data) {
		return new Result(code, message, data);
	}

	public Result result(String message, Object data) {
		return new Result(200, message, data);
	}

	public Result result(Integer code, String message) {
		return new Result(code, message);
	}

	public Result result(Integer code) {
		return new Result(code);
	}

	public Result result() {
		return new Result(200, "操作成功");
	}

	public Result success() {
		return this.result();
	}

	public Result success(String message, Object data) {
		return this.result(message, data);
	}

	public Result success(Object data) {
		return this.result(data);
	}

	public Result error() {
		return this.result(500);
	}

	public Result error(String message) {
		return this.result(500, message);
	}

	public Result toGrid(PageInfo<?> pageInfo) {
		PageResult pageResult = null == pageInfo ? new PageResult() : new PageResult(pageInfo.getTotal(), (List) (pageInfo.getList() == null ? new ArrayList() : pageInfo.getList()));
		return success(pageResult);
	}

	//vue-shop中ShopResult
	public ShopResult shopSuccess(Integer code ,String msg,Object data) {
		ShopResult shopResult = new ShopResult();
		Map<String, Object> meta = shopResult.getMeta();
		meta.put("msg", msg);
		meta.put("status", code);
		shopResult.setData(data);
		return shopResult;
	}

	public ShopResult shopSuccess(Integer code) {
		ShopResult shopResult = new ShopResult();
		Map<String, Object> meta = shopResult.getMeta();
		meta.put("msg", "成功");
		meta.put("status", 200);
		shopResult.setData(null);
		return shopResult;
	}

	public ShopResult shopError() {
		ShopResult shopResult = new ShopResult();
		Map<String, Object> meta = shopResult.getMeta();
		meta.put("msg", "系统繁忙，请稍后重试");
		meta.put("status", 500);
		shopResult.setData(null);
		return shopResult;
	}
	
}
